package com.shg.web.action;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.shg.bean.Book;
import com.shg.bean.BookType;
import com.shg.bean.PageView;
import com.shg.bean.Shop;
import com.shg.bean.Users;
import com.shg.service.BookService;
import com.shg.service.BookTypeService;
import com.shg.service.ShopService;
import com.shg.web.tools.WebTool;


@Controller
public class BookAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//
	private String message;
	//书籍对象
	private Book book;
	//店铺
	private Shop shop;
	//成功调整页面信息
	private String massage;
	//当前页面
	private Integer page=1;
	//每页显示记录数
	private Integer maxrecord=3;
	//页面信息
	private PageView<Book> pageView;
	//书籍类型
	private Integer type;
	//类别ID
	private String typeId;
	//描述
	private String description;
	//书籍类别
	private List<BookType> types;
	//书籍的ID
	private String[] bookid;
	//书籍的状态
	private int state;
	//注入服务对象
	@Resource BookService bookService;
	//书籍类别
	@Resource BookTypeService bookTypeService;
	//店铺
	@Resource ShopService shopService;
	//存储导航的键值map
	private Map<String, String> mapNavigation;
	//跳转URL
	private String httpUrl;
	//控制选择
	private Integer flag;
	//
	private List<Book> books;
	/**
	 * 主页显示(买家)
	 * @return
	 */
	public String index(){
		return "index";
	}
	/**
	 * 开店
	 */
	public String openshop(){
		try {
			shop= (Shop)shopService.select(Shop.class, WebTool.getLogin().getId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		if(shop==null){
			return "openshop";
		}else{
			return "fitment";
		}
	}
	/**
	 * 店铺注册
	 * 如果该用户已经注册过了只能装需
	 */
	public String shoprigster(){
		return "shoprigster";
	}
	/**
	 * 添加书籍页面的迁移
	 * @return
	 */
	public String managerUI(){
		shop = WebTool.getLogin().getShop();
		return "managerUI";
	}
	/**
	 * 添加书籍的功能界面
	 * @return
	 */
	public String addBookUI(){
		try {
			shop= (Shop)shopService.select(Shop.class, WebTool.getLogin().getId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		if(shop==null){
			return "openshop";
		}else{
			return "addBookUI";
		}
	}
	/**
	 * 添加书籍数据到数据库
	 * @return
	 */
	public String addBook(){
		Users users = WebTool.getLogin();
		if(users!=null){
			Shop shop=new Shop();
			shop.setId(users.getId());
			book.setShop(shop);
			
			bookService.insert(book);
			this.massage="添加书籍成功~";
			return "success";
		}
		return "login";
	}
	/**
	 * 创库中待上架的书籍
	 * @return
	 */
	public String bookshelf(){
		Object[] parameters=new Object[]{type};
		book("show=?",parameters);
		return "bookshelf";
	}
	/**
	 * 正在销售中的书籍
	 * 当show=1 表示正在销售中的书籍
	 * @return
	 */
	public String sellBook(){
		Users users = WebTool.getLogin();
		if(users!=null){
			if(users.getShop()!=null){
				Object[] parameters=new Object[]{type,users.getShop().getId()};
				book("show=? and shop_id=?",parameters);
			}
		}
		if(type==1){//上架
			return "sellBook";
		}else if(type==2){//待审核
			return "auditingBook";
		}else if(type==3){//删除的书籍
			return "badBook";
		}else if(type==4){//禁书
			return "estopBook";
		}
		return null;
	}
	
	private void book(String where,Object[] parameters){
		pageView=new PageView<Book>(maxrecord,page);
		try {
			pageView.setQueryResult(bookService.getPagination(Book.class, pageView.getFirstResult(), pageView.getMaxrecord(), where, parameters));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
		}
	}

	/**
	 * 组装导航
	 * @param str
	 * @param type
	 
	private void navigation(BookType type){
		if(type.getParent()!=null){
			navigation(type.getParent());
			//注意这里的位置一定不能颠倒
			mapNavigation.put(type.getParent().getId(), type.getParent().getName());
		}
	}*/
	/**
	 * 控制调用方法
	 * @return
	 */
	public String controlType(){
		if(flag==0){
			 recommend();
		}else if(flag==1){
			bookShow();
		}
		if(type==1){
			return "sellBook";
		}else if(type==2){
			return "auditingBook";
		}
		return null;
	}
	/**
	 * 推荐、取消推荐书籍
	 * @return
	 */
	public String recommend(){
		try {
			bookService.updateRecommend(bookid, state);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		Users users=WebTool.getLogin();
		if(users!=null){
			Object[] parameters=new Object[]{type,users.getShop().getId()};
			book("show=? and shop_id=?",parameters);
		}
		return null;
	}
	/**
	 * 书籍的状态
	 * 1.上架、2.下架，3.问题书籍，4、售出书籍
	 * @return
	 */
	public String bookShow(){
		try {
			bookService.updateShow(bookid, state);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		Users users=WebTool.getLogin();
		if(users!=null){
			Object[] parameters=new Object[]{type,users.getShop().getId()};
			book("show=? and shop_id=? ",parameters);
		}
		return null;
	}
	/**
	 * 店铺注册
	 * @return
	 */
	public String shopRegister(){
		shop.setUsers(WebTool.getLogin());
		shopService.insert(shop);
		this.message="开店成功";
		return "shopRegister";
	}
	
	
	public String getHttpUrl() {
		return httpUrl;
	}
	public void setHttpUrl(String httpUrl) {
		this.httpUrl = httpUrl;
	}
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	public String getMassage() {
		return massage;
	}
	public void setMassage(String massage) {
		this.massage = massage;
	}
	public PageView<Book> getPageView() {
		return pageView;
	}
	public void setPageView(PageView<Book> pageView) {
		this.pageView = pageView;
	}
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getMaxrecord() {
		return maxrecord;
	}
	public void setMaxrecord(Integer maxrecord) {
		this.maxrecord = maxrecord;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public Shop getShop() {
		return shop;
	}
	public void setShop(Shop shop) {
		this.shop = shop;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public List<BookType> getTypes() {
		return types;
	}
	public void setTypes(List<BookType> types) {
		this.types = types;
	}
	public String getTypeId() {
		return typeId;
	}
	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}
	public Map<String, String> getMapNavigation() {
		return mapNavigation;
	}
	public void setMapNavigation(Map<String, String> mapNavigation) {
		this.mapNavigation = mapNavigation;
	}
	public String[] getBookid() {
		return bookid;
	}
	public void setBookid(String[] bookid) {
		this.bookid = bookid;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Integer getFlag() {
		return flag;
	}
	public void setFlag(Integer flag) {
		this.flag = flag;
	}
	public List<Book> getBooks() {
		return books;
	}
	public void setBooks(List<Book> books) {
		this.books = books;
	}
	
}
